package com.vcc.channel.base.channel;

import cn.hutool.core.util.ClassUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.List;

@EqualsAndHashCode(callSuper = true)
@Data
public class LlResult extends BaseResult {

    private Integer code;

    private String message;

    private Object data;

    private Boolean success;



    public boolean hasSuccess() {
        return 100001 == code;
    }

    public boolean hasError() {
        return !hasSuccess();
    }

    public <T> T dataToEntity(Class<T> tClass) {
        if (this.data instanceof String) {
            return JSONUtil.toBean((String) this.data, tClass);
        } else if (ClassUtil.isBasicType(this.data.getClass())) {
            return (T) this.data;
        } else {
            return JSONUtil.toBean(JSONUtil.toJsonStr(this.data), tClass);
        }
    }

    public <T> List<T> dataToListEntity(Class<T> tClass) {
        if (this.data instanceof String) {
            return JSONUtil.parseArray((String) this.data, false).toList(tClass);
        } else {
            return JSONUtil.parseArray(JSONUtil.toJsonStr(this.data), false).toList(tClass);
        }
    }
}
